import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router'
import axios from 'axios';
import {UserService} from '../user.service';


@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})


export class LoginComponent implements OnInit {

  //@Output()  changeUser:any

  // @Output()  outer  =  new EventEmitter();


  constructor(private route: ActivatedRoute, private router: Router, private UserService: UserService) {
  }

  ngOnInit(): void {
  }


  login(formobj){
    console.log(formobj.value)

    var params = new URLSearchParams()
    params.append('username', formobj.value.username)
    params.append('password', formobj.value.password)
    params.append('role', formobj.value.role)


    this.getData(params, "/user/login" );

  }

  reg(formobj){
    this.router.navigate(['/reg']);
  }
  public async getData(params:URLSearchParams, url:String){
    let baseUrl = "http://localhost:8999";
    await  axios.post(baseUrl+url, params, {withCredentials: true})
    .then((response) => {
          //console.log(response);
         // window.localStorage.setItem('user',JSON.stringify(response.data))
          this.UserService.getCurrentUser()

          this.router.navigate(['/']);

      return response;
    })
    .catch(function (error) {
      console.log(error);
    });
}


}
